﻿namespace ShenNius.Share.Infrastructure.Common
{
    public class IpParseHelper
    {
        public static string GetAddressByIP(string ip)
        {
            if (string.IsNullOrEmpty(ip)) return string.Empty;
            var url = $"http://whois.pconline.com.cn/ipJson.jsp";
            using (var http = new HttpUtil(url))
            {
                var model = http
                    .CreateRequest()
                    .AddStringParts(new { json = "true", ip = ip })
                    .AddString("ip", ip)
                    .GetResult<Ipinfos>();

                return $"{model.ip}-{model.addr}";
            }
        }

        /// <summary>
        /// ip信息实体类
        /// </summary>
        public class Ipinfos
        {
            public string ip { get; set; }
            public string pro { get; set; }
            public string proCode { get; set; }
            public string city { get; set; }
            public string cityCode { get; set; }
            public string region { get; set; }
            public string regionCode { get; set; }
            public string addr { get; set; }
            public string regionNames { get; set; }
            public string err { get; set; }
        }

    }
}
